package com.ebei.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ebei.admin.mapper.SysDictDetailMapper;
import com.ebei.admin.mapper.SysDictMapper;
import com.ebei.admin.model.entity.SysDict;
import com.ebei.admin.model.entity.SysDictDetail;
import com.ebei.admin.service.SysDictDetailService;
import com.ebei.admin.service.SysDictService;
import com.ebei.common.constant.CommonConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("sysDictDetailService")
public class SysDictDetailServiceImpl extends ServiceImpl<SysDictDetailMapper,SysDictDetail> implements SysDictDetailService {

    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysDictMapper sysDictMapper;

    @Override
    public List<SysDictDetail> findDictDetailsByDictId(Integer dictId) {
        SysDictDetail sysDictDetail = new SysDictDetail();
        sysDictDetail.setDelFlag(CommonConstant.STATUS_NORMAL);
        sysDictDetail.setDictId(dictId);
        return super.selectList(new EntityWrapper<>(sysDictDetail));
    }

    @Override
    public List<SysDictDetail> findDictDetailsByDictCode(String dictCode) {
        try {
            SysDict condition = new SysDict();
            condition.setDictCode(dictCode);
            condition.setDelFlag(CommonConstant.STATUS_NORMAL);
            SysDict sysDict = this.sysDictService.selectOne(new EntityWrapper<>(condition));

            if(sysDict == null){
                return new ArrayList<>();
            }

            return findDictDetailsByDictId(sysDict.getDictId());

        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

    @Override
    public List<SysDictDetail> findDictDetailsByDictCodeAndCompanyId(String dictCode, Integer companyId) {
        try {
            SysDict condition = new SysDict();
            condition.setDictCode(dictCode);
            condition.setCompanyId(companyId);
            SysDict sysDict = sysDictService.selectOne(new EntityWrapper<>(condition));
            return findDictDetailsByDictId(sysDict.getDictId());
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
